package com.twb.auth.controller;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.twb.auth.entity.twbTenantInfo.TwbTenantInfo;
import com.twb.auth.entity.twbTenantInfo.dto.GetTwbTenantInfoByIdDto;
import com.twb.auth.entity.twbTenantInfo.dto.SearchTwbTenantInfoDto;
import com.twb.auth.entity.twbTenantInfo.vo.SaveTwbTenantInfoVo;
import com.twb.auth.entity.twbTenantInfo.vo.SearchTwbTenantInfoVo;
import com.twb.auth.entity.twbTenantInfo.vo.UpdateTwbTenantInfoByIdVo;
import com.twb.auth.service.ITwbTenantInfoService;
import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.Results;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Title  TwbTenantInfoController
 * @Description 租户信息
 * 
 * @author DSY
 * @date 2019-12-01 22:31:02
 */
@RestController
@RequestMapping("/sys/twbTenantInfo")
@Api(tags = "公司信息", value = "TwbTenantInfoController")
public class TwbTenantInfoController extends BaseController {
	@Autowired
	ITwbTenantInfoService twbTenantInfoService;

	/**
	 * @Title  searchTwbTenantInfo
	 * @Description 分页查询
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2019-12-01 22:31:02
	 */
	@GetMapping("/search")
	@ApiOperation(value = "分页查询")
	public Result<TwbPageInfo<SearchTwbTenantInfoDto>> searchTwbTenantInfo(SearchTwbTenantInfoVo vo) {
		TwbPageInfo<SearchTwbTenantInfoDto> result = twbTenantInfoService.searchTwbTenantInfo(vo);
		return Results.successWithData(result);
	}

	/**
	 * @Title  getTwbTenantInfoById
	 * @Description 根据ID获取详情
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2019-12-01 22:31:02
	 */
	@GetMapping("/getById")
	@ApiOperation(value = "根据ID获取详情")
	public Result<GetTwbTenantInfoByIdDto> getTwbTenantInfoById(
			@NotBlank @RequestParam @ApiParam(value = "序号", name = "id", required = true) String id) {
		TwbTenantInfo TwbTenantInfo = twbTenantInfoService.getById(id);
		GetTwbTenantInfoByIdDto dto = new GetTwbTenantInfoByIdDto();
		BeanUtils.copyNotNullProperties(TwbTenantInfo, dto);
		return Results.successWithData(dto);
	}

	/**
	 * @Title  saveTwbTenantInfo
	 * @Description 保存信息
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2019-12-01 22:31:02
	 */
	@PostMapping("/save")
	@ApiOperation(value = "保存信息")
	public Result<String> saveTwbTenantInfo(@RequestBody @Valid SaveTwbTenantInfoVo vo) {
		TwbTenantInfo record = new TwbTenantInfo();
		record.setCode(vo.getCode());
		record.setIsdel(0);
		long count = twbTenantInfoService.count(record);
		if (count > 0) {
			return Results.failure("[" + vo.getCode() + "]已存在");
		}
		boolean result = twbTenantInfoService.saveTwbTenantInfo(vo);
		if (result) {
			return Results.success("保存成功");
		} else {
			return Results.failure("保存失败");
		}
	}

	/**
	 * @Title  updateTwbTenantInfoById
	 * @Description 更新信息
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2019-12-01 22:31:02
	 */
	@PutMapping("/updateById")
	@ApiOperation(value = "更新信息")
	public Result<String> updateTwbTenantInfoById(@RequestBody @Valid UpdateTwbTenantInfoByIdVo vo) {
		boolean result = twbTenantInfoService.updateTwbTenantInfoById(vo);
		if (result) {
			return Results.success("更新成功");
		} else {
			return Results.failure("更新失败");
		}
	}

	/**
	 * @Title  deleteTwbTenantInfoById
	 * @Description 删除信息
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2019-12-01 22:31:02
	 */
	@PostMapping("/deleteById")
	@ApiOperation(value = "删除信息")
	public Result<String> deleteTwbTenantInfoById(
			@NotBlank @RequestParam @ApiParam(value = "序号", name = "id", required = true) String id) {
		boolean result = twbTenantInfoService.deleteTwbTenantInfoById(id);
		if (result) {
			return Results.success("删除成功");
		} else {
			return Results.failure("删除失败");
		}
	}

	/**
	 * @Title  deleteBatchTwbTenantInfoByIds
	 * @Class: TwbTenantInfoController.java
	 * @Description 批量删除信息
	 * @param ids
	 * @return
	 * @author DSY
	 * @date 2019年12月1日 下午10:48:36
	 */
	@PostMapping("/batchDeleteByIds")
	@ApiOperation(value = "批量删除信息")
	public Result<String> deleteBatchTwbTenantInfoByIds(
			@NotBlank @RequestParam @ApiParam(value = "序号", name = "id", required = true) String ids) {
		boolean result = twbTenantInfoService.deleteTwbTenantInfoByIds(ids);
		if (result) {
			return Results.success("删除成功");
		} else {
			return Results.failure("删除失败");
		}
	}

	/**
	 * @Title  logicDeleteTwbTenantInfoById
	 * @Description 逻辑删除信息
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2019-12-01 22:31:02
	 */
	@PostMapping("/logicDeleteById")
	@ApiOperation(value = "逻辑删除信息")
	public Result<String> logicDeleteTwbTenantInfoById(@NotBlank @RequestParam String id) {
		TwbTenantInfo record = twbTenantInfoService.getById(id);
		if (null == record) {
			return Results.failure("信息不存在");
		}
		record = new TwbTenantInfo();
		record.setId(id);
		twbTenantInfoService.setUpdateInfo(record);
		record.setIsdel(1);
		boolean result = twbTenantInfoService.updateById(record);
		if (result) {
			return Results.success("删除成功");
		} else {
			return Results.failure("删除失败");
		}
	}

	/**
	 * @Title  logicBatchDeleteTwbTenantInfoByIds
	 * @Class: TwbTenantInfoController.java
	 * @Description 批量逻辑删除
	 * @param ids
	 * @return
	 * @author DSY
	 * @date 2019年12月1日 下午10:59:43
	 */
	@PostMapping("/logicBatchDeleteByIds")
	@ApiOperation(value = "逻辑删除信息")
	public Result<String> logicBatchDeleteTwbTenantInfoByIds(@NotBlank @RequestParam String ids) {
		int result = twbTenantInfoService.logicBatchDeleteTwbTenantInfoByIds(ids);
		if (result > 0) {
			return Results.success("删除成功");
		} else {
			return Results.failure("删除失败");
		}
	}

	/**
	 * @Title  exportExcel
	 * @Description 导出excel
	 * @param searchVo
	 * @param response
	 * @return
	 * @author DSY
	 * @date 2019-12-01 22:31:02
	 */
	@Auth(verifyURL = false)
	@PostMapping("/exportExcel")
	@ApiOperation(value = "导出excel")
	public void exportExcel(@RequestBody SearchTwbTenantInfoVo searchVo, HttpServletResponse response) {
		twbTenantInfoService.exportExcel(searchVo, response);
	}
}
